﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace MainService
{
    public class CDATA : IXmlSerializable
    {
        
        public CDATA()
        {
        }
        
        public CDATA(string innerxml) 
        {
            this.InnerXML = innerxml;
        }
        
        public string InnerXML { get; set; }
        
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }
        
        public static char[]  CharTrims= new char[] { '\r', '\n', '\t', ' '};
        
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            string source = reader.ReadInnerXml();
            string startTag = "<![CDATA[";
            string endTag = "]]>";
            source = source.Trim(CharTrims);
            if (source.StartsWith(startTag) && source.EndsWith(endTag))
            {
                source = source.Substring(startTag.Length, source.LastIndexOf(endTag) - startTag.Length);
            }
            string innerxml = source.Trim(CharTrims);
            this.InnerXML = innerxml;
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            writer.WriteCData(this.InnerXML);
        }

    }
}
